﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FinanSoft.Utilities;
using FinanSoft.Entities;
using FinanSoft.BusinessLayer;

namespace FinanSoft.PresentacionInventario
{
    public partial class frmItemTransfer : Form
    {

        private int[] _storeIds;
        private int[] _itemIds;
        
        public frmItemTransfer()
        {
            InitializeComponent();
            //Llena los combos
            fillStoreRooms();
            fillItems();
        }

        /// <summary>
        /// Llena el combobox con las bodegas a consultar
        /// </summary>
        private void fillStoreRooms()
        {
            cmbOriginStore.Items.Clear();
            cmbDestinyStore.Items.Clear();
            List<StoreRoom> stores = StoreRoomManager.GetInstance().GetStoreRooms();
            if (!StoreRoomManager.GetInstance().IsError)
            {
                _storeIds = new int[stores.Count];
                for (int ele = 0; ele < stores.Count; ele++)
                {
                    _storeIds[ele] = stores[ele].Id;
                    cmbOriginStore.Items.Add(stores[ele].Name);
                    cmbDestinyStore.Items.Add(stores[ele].Name);
                }
            }
            else
            {
                MessageBox.Show(StoreRoomManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
            cmbOriginStore.SelectedIndex = 0;
            cmbDestinyStore.SelectedIndex = cmbDestinyStore.Items.Count - 1; 
        }

        /// <summary>
        /// Llena el combobox con las bodegas a consultar
        /// </summary>
        private void fillItems()
        {
            cmbArticulo.Items.Clear();

            List<Item> items = ItemManager.GetInstance().GetItems();
            if (!ItemManager.GetInstance().IsError)
            {
                _itemIds = new int[items.Count];

                for (int ele = 0; ele < items.Count; ele++)
                {
                    _itemIds[ele] = items[ele].Id;
                    cmbArticulo.Items.Add(items[ele].Descripción);
                }
            }
            else
            {
                MessageBox.Show(StoreRoomManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }

            cmbArticulo.SelectedIndex = 0;
        }

        private void btnSalir_Click(object sender, EventArgs e)
        {
            Close();
        }

        private bool validateInformation()
        {
            return (cmbArticulo.SelectedIndex != Constants.INVALID_ID && cmbOriginStore.SelectedIndex != Constants.INVALID_ID &&
               cmbDestinyStore.SelectedIndex != Constants.INVALID_ID && txtCantidad.Text.Trim().Length > 0);
        }

        private void btnActInfo_Click(object sender, EventArgs e)
        {
            //Extrae el id de la bodega seleccionada
            if(validateInformation())
            {
                bool result = ItemManager.GetInstance().MakeItemTransfer(
                        _itemIds[cmbArticulo.SelectedIndex], _storeIds[cmbDestinyStore.SelectedIndex],
                        _storeIds[cmbOriginStore.SelectedIndex], Convert.ToInt32(txtCantidad.Text));
                if(result)
                    MessageBox.Show(MessageConstants.INVENTORY_OP_SUCCESS, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                else
                    MessageBox.Show(ItemManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
                MessageBox.Show(MessageConstants.MISSING_FIELDS, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void txtCantidad_KeyPress(object sender, KeyPressEventArgs e)
        {
            const char Delete = (char)8;
            e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
        }
    }
}
